package ar.edu.unicen.pladema.vc1.practico1;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;

import ar.edu.unicen.pladema.main.SWTUtils;
import ar.edu.unicen.pladema.ui.model.ColorRange;
import ar.edu.unicen.pladema.ui.mywidgets.ColorList;
import ar.edu.unicen.pladema.ui.mywidgets.FileSelector;
import ar.edu.unicen.pladema.ui.panel.AbstractPanel;

public class P1Enunciado4 extends AbstractPanel {
	private Composite lienzo;
	private Button recargar;
	//private Button agregar;
	private Button limpiar;
	private Button paleta1;
	private Button paleta2;
	private P1Enunciado4Drawer listener;
	private Label opciones;
	private FileSelector fileSel;
	private ColorList colorList;
	private Label limites;
	private Label punto;
	
	public P1Enunciado4(Composite parent) {
		super(parent);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void createWidgets() {
		opciones = new Label(this, SWT.NONE);
		limites = new Label(this, SWT.NONE);
		punto = new Label(this, SWT.NONE);
		opciones.setText("OPCIONES");
		limites.setText("imagen no cargada");
		punto.setText("seleccione un punto");
		Font f = new Font(this.getDisplay(), "Arial", 24, SWT.BOLD);
		opciones.setFont(f);
		lienzo = new Composite(this, SWT.BORDER);
		listener = new P1Enunciado4Drawer(this);
		listener.setLabelInfo(limites);
		// TODO Auto-generated method stub
		lienzo.addPaintListener(listener);
		lienzo.addMouseListener(new MouseAdapter() {
			
			@Override
			public void mouseDown(MouseEvent arg0) {
				int p = listener.getLandHeight(arg0.x, arg0.y);
				String text = "Dibuje la imagen";
				if (p != -1) {
					text = Integer.toString(p); 
				}
				punto.setText(text);
			}
		});

		recargar = SWTUtils.createButton(this, "DRAW", new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				doTheDraw();
				
			}
		});
		
		fileSel = new FileSelector(this);
		colorList = new ColorList(this);
		colorList.setColorRange(listener.testPaleta());
		
		limpiar = SWTUtils.createButton(this, "CLEAN", new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				clean(listener.basicPaleta());
				
			}
		});
		paleta1 = SWTUtils.createButton(this, "PALETA1", new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				clean(P1Enunciado4Drawer.testPaleta2());
				
			}
		});
		paleta2 = SWTUtils.createButton(this, "PALETA2", new Listener() {
			@Override
			public void handleEvent(Event arg0) {
				clean(P1Enunciado4Drawer.testPaleta3());
				
			}
		});
	}

	private void doTheDraw() {
		//listener.setSize(rectSize.getWidth(), rectSize.getHeight());
		//listener.setColors(inicio.getColor(), medio.getColor(), fin.getColor());
		listener.setHGTFile(fileSel.getFile());
		ColorRange cr = colorList.getColorRange();
		cr.printList();
		listener.setColorRange(cr);
		lienzo.redraw();
		
	}
	protected void refrescar() {
		layout();
	}
	protected void clean(ColorRange colorRange) {
		listener.setColorRange(colorRange);
		colorList.setColorRange(colorRange);
		refrescar();
		lienzo.redraw();
	}
	
	@Override
	protected void layoutWidgets() {
		setLayout(new FormLayout());
		fileSel.setLayoutData(SWTUtils.createTopFormData());

		FormData fdLienzo = new FormData();
		fdLienzo.top = new FormAttachment(fileSel);
		fdLienzo.left = new FormAttachment(0);
		fdLienzo.right = new FormAttachment(opciones);
		fdLienzo.bottom = new FormAttachment(SWTUtils.MAX_FORMDATA_COORD);
		
		lienzo.setLayoutData(fdLienzo);
		
		opciones.setLayoutData(SWTUtils.createRightFormData(fileSel, 5));
		limites.setLayoutData(SWTUtils.createRightFormData(opciones, 5));
		punto.setLayoutData(SWTUtils.createRightFormData(limites, 5));
		colorList.setLayoutData(SWTUtils.createRightFormData(punto, 5));
		limpiar.setLayoutData(SWTUtils.createRightFormData(colorList, 5));
		paleta1.setLayoutData(SWTUtils.createRightFormData(limpiar, 5));
		paleta2.setLayoutData(SWTUtils.createRightFormData(paleta1, 5));
		recargar.setLayoutData(SWTUtils.createRightFormData(paleta2, 5));
	}

}
